#include "DRPPlugin.h"
#include <SEDRPPlugin/GBuffer.h>
#include <SERender/Resourses/Loader/TextureLoader.h>
#include <SEDRPPlugin/GBufferRenderPass.h>
#include <SEDRPPlugin/LightingRenderPass.h>
#include <SEDRPPlugin/DepthCubeMapRenderPass.h>

namespace SEngine
{
    DRPPlugin::DRPPlugin(Renderer &renderer)
        : RenderPipline(renderer)
    {
        GBuffer buffer;
        
        TextureLoader<Texture2D> texLoader;             // 2D贴图生成器
        TextureLoader<TextureCube> cubeTexLoader;       // 立方体贴图生成器

        // PBR基本数据
        texLoader.SetInternalFormat(RGBA32F);
        texLoader.SetFormat(RGBA);
        texLoader.SetType(FLOAT);
        texLoader.SetParameter(TEXTURE_MAG_FILTER, LINEAR);
        texLoader.SetParameter(TEXTURE_MIN_FILTER, LINEAR);
        texLoader.SetParameter(TEXTURE_WRAP_S, CLAMP_TO_EDGE);
        texLoader.SetParameter(TEXTURE_WRAP_T, CLAMP_TO_EDGE);
        buffer.AddRenderTarget("PositionMetalRT", texLoader.CreateTexture(1024, 1024, nullptr));
        buffer.AddRenderTarget("AlbedoRoughRT", texLoader.CreateTexture(1024, 1024, nullptr));
        buffer.AddRenderTarget("NormalDepthRT", texLoader.CreateTexture(1024, 1024, nullptr));

        // // 阴影渲染准备
        // cubeTexLoader.SetInternalFormat(RGB32F);
        // cubeTexLoader.SetFormat(RGB);
        // cubeTexLoader.SetType(FLOAT);
        // cubeTexLoader.SetParameter(TEXTURE_MAG_FILTER, LINEAR);
        // cubeTexLoader.SetParameter(TEXTURE_MIN_FILTER, LINEAR);
        // cubeTexLoader.SetParameter(TEXTURE_WRAP_S, CLAMP_TO_EDGE);
        // cubeTexLoader.SetParameter(TEXTURE_WRAP_T, CLAMP_TO_EDGE);
        // cubeTexLoader.SetParameter(TEXTURE_WRAP_R, CLAMP_TO_EDGE);
        // buffer.AddRenderTarget("DepthCubeMapRT", cubeTexLoader.CreateTexture());

        // texLoader.SetInternalFormat(RGB16F);
        // texLoader.SetFormat(RGB);
        // texLoader.SetType(FLOAT);
        // texLoader.SetParameter(TEXTURE_MAG_FILTER, LINEAR);
        // texLoader.SetParameter(TEXTURE_MIN_FILTER, LINEAR);
        // texLoader.SetParameter(TEXTURE_WRAP_S, CLAMP_TO_EDGE);
        // texLoader.SetParameter(TEXTURE_WRAP_T, CLAMP_TO_EDGE);
        // buffer.AddRenderTarget("ShadowMapRT", texLoader.CreateTexture());           // 记录场景中某点被阴影覆盖的程度


        m_renderer.AnyDescriptor::AddDescriptor<GBuffer>(std::move(buffer));
    }

    void DRPPlugin::Init()
    {
        this->NextPass<GBufferRenderPass>();
        // this->NextPass<DepthCubeMapRenderPass>();
        this->NextPass<LightRenderPass>();
    }

    void DRPPlugin::Deinit()
    {
        this->Reset();
    }

    IPlugin *CreatePlugin(void* args)
    {
        return new DRPPlugin(*(Renderer*)args);
    }

    void DestroyPlugin(IPlugin *plugin)
    {
        delete plugin;
    }


} // namespace SEngine
